// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   DBUtil.java

package WEB-INF.classes.com.actimind.actitime;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

// Referenced classes of package com.actimind.actitime.util:
//            Assert

public class DBUtil
{

    public DBUtil()
    {
    }

    public static String lengthFunction(Connection con, String param)
        throws SQLException
    {
        DatabaseMetaData meta = con.getMetaData();
        String productName = meta.getDatabaseProductName();
        return dbFunction(productName, "LENGTH") + "(" + param + ")";
    }

    public static String dayOfWeekFunction(Connection con, String param)
        throws SQLException
    {
        DatabaseMetaData meta = con.getMetaData();
        String productName = meta.getDatabaseProductName();
        return dbFunction(productName, "DAYOFWEEK") + "(" + param + ")";
    }

    private static String dbFunction(String dbname, String functionKey)
    {
        if("ACCESS".equals(dbname))
            return (String)accessFunctions.get(functionKey);
        else
            return (String)mySqlFunctions.get(functionKey);
    }

    public static String getSQLSwitchExpression(Connection con, String firstExpression, String firstResult, String secondExpression, String secondResult)
        throws SQLException
    {
        Assert.notNull(firstExpression, firstResult, secondResult);
        DatabaseMetaData meta = con.getMetaData();
        String productName = meta.getDatabaseProductName();
        String func = dbFunction(productName, "SWITCH");
        if("ACCESS".equals(productName))
            return func + "(" + firstExpression + ", " + firstResult + ", " + secondExpression + ", " + secondResult + ")";
        else
            return func + "(" + firstExpression + ", " + firstResult + ", " + secondResult + ")";
    }

    public static boolean isAccess(Connection con)
        throws SQLException
    {
        DatabaseMetaData meta = con.getMetaData();
        return "ACCESS".equals(meta.getDatabaseProductName());
    }

    public static final String DB_NAME_ACCESS = "ACCESS";
    private static final String LENGTH_FUNC_KEY = "LENGTH";
    private static final String DAYOFWEEK_FUNC_KEY = "DAYOFWEEK";
    private static final String SWITCH_FUNC_KEY = "SWITCH";
    static Map accessFunctions;
    static Map mySqlFunctions;

    static 
    {
        accessFunctions = new HashMap();
        accessFunctions.put("LENGTH", "len");
        accessFunctions.put("DAYOFWEEK", "WeekDay");
        accessFunctions.put("SWITCH", "Switch");
        mySqlFunctions = new HashMap();
        mySqlFunctions.put("LENGTH", "length");
        mySqlFunctions.put("DAYOFWEEK", "dayofweek");
        mySqlFunctions.put("SWITCH", "if");
    }
}
